import 'package:flutter/material.dart'; // Material UI组件库
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shop/http/HttpUtil.dart';
import 'dart:ui';
import 'package:shop/route/my_route.dart';
import 'package:shop/shop/rider/orderPages/AllOfOrder.dart';
import 'package:shop/shop/rider/orderPages/WanCheng.dart';
import 'package:shop/shop/rider/orderPages/WeiFaHuo.dart';
import 'package:shop/shop/rider/orderPages/WeiFuKuan.dart';
import 'package:shop/shop/rider/orderPages/WeiShouHuo.dart';
import 'package:shop/shop/rider/orderPages/ZiTi.dart';

import 'Login.dart';

class OrderListPage extends StatefulWidget {
  final String showIndex;
  OrderListPage({Key key, this.showIndex}) : super(key: key);
  @override
  State<StatefulWidget> createState() {
    return new _OrderListPage();
  }
}

class _OrderListPage extends State<OrderListPage> with SingleTickerProviderStateMixin {

  //构造方法
  _OrderListPage(){
    isLogin();
  }

  // 判断是否登录
  Future<void> isLogin() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    if(prefs.getBool("isLogin")==false){
      Fluttertoast.showToast(
        msg: "请先登录",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.black45,
        textColor: Colors.white,
        fontSize: 16.0
      );
      MyRoute.replace(Login());
    }
    String riderId = prefs.getString("riderId");
    var x = await HttpUtil().get("/waimai/mOder/getRiderOrderNum?riderId="+riderId);
    if(x['code']==200){
      print("新任务："+x['data']['xingrenwu'].toString());
      print("待取货："+x['data']['daiquhuo'].toString());
      print("待送达："+x['data']['daisongda'].toString());
      print("已完成："+x['data']['yiwancheng'].toString());
      titleTabs.removeWhere((element) => true); // 清空
      // 重新添加
      titleTabs.add(Tab(text: '新任务('+x['data']['xingrenwu'].toString()+")",));
      titleTabs.add(Tab(text: '待取货('+x['data']['daiquhuo'].toString()+")",));
      titleTabs.add(Tab(text: '待送达('+x['data']['daisongda'].toString()+")",));
      titleTabs.add(Tab(text: '已完成('+x['data']['yiwancheng'].toString()+")",));
      setState(() {

      });
    }
  }

  TabController mController;// tab控制器
  // int showIndex;
  // _OrderListPage(this.showIndex);
  // List<Tab> titleTabs = <Tab>[];
  List<Tab> titleTabs = <Tab>[
    Tab(text: '新任务(2)',),
    Tab(text: '待取货(3)',),
    Tab(text: '待送达(1)',),
    // Tab(text: "待收货",),
    // Tab(text: "待自提",),
    Tab(text: "已完成",),
  ];

  @override
  void initState() {
    super.initState();
    mController = TabController(initialIndex: int.parse(widget.showIndex),length:4, vsync: this);
    // 初始化TabController
    // 参数1：初试显示的tab位置
    // 参数2：tab的个数
    // 参数3：动画效果的异步处理，默认格式

    // 添加监听器
    mController.addListener(() => _onTabChanged());

  }

  @override
  Widget build(BuildContext context) {
    return  new Scaffold(
      // 创建TabBar & 设置属性
      appBar: AppBar(
        title:Text("我的订单"),
        elevation: 0,
        bottom:TabBar(
          controller: mController,// 设置控制器
          labelColor: Colors.white, //选中的颜色
          labelStyle: TextStyle(fontSize: 16), //选中的样式
          unselectedLabelColor: Colors.black, //未选中的颜色
          unselectedLabelStyle: TextStyle(fontSize: 14), //未选中的样式
          indicatorColor: Colors.white, //下划线颜色
          isScrollable: true, //是否可滑动，设置不可滑动，则是tab的宽度等长
          //tab标签
          tabs: titleTabs, // 设置标题
          //点击事件
          onTap: (int i) {
            print(i+10);
          },
        ) ,
      ),
      body: Container(
        padding: EdgeInsets.only(top:20),
        child: new TabBarView(
        controller: mController,
        children:  <Widget>[ // 每个空间对应的页面
          AllOfOrder(titleTabs),  // 新任务
          WeiFuKuan(titleTabs),   // 待取货
          WeiFaHuo(titleTabs),    // 带送达
         //  WeiShouHuo(titleTabs),
         //  ZiTi(titleTabs),
         WanCheng(titleTabs)    // 已完成
        ],
      )
        ,)
    );
  }

  @override
  void dispose() {
    super.dispose();
    mController.dispose(); // 当整个页面dispose时，记得把控制器也dispose掉，释放内存
  }

  // 点击监听函数
  _onTabChanged() {
    print(mController.index);
  }
}